﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using Sunny.UI.Demo;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace NonStandardEqpMonitor
{
    public partial class FormMain : UIForm
    {
        Dictionary<int, string> pageSet = new Dictionary<int, string>();
        public FormMain()
        {
            InitializeComponent();
            this.MainTabControl = uiTabControl1;
            uiNavBar1.TabControl = uiTabControl1;
            uiNavMenu1.TabControl = uiTabControl1;

            uiTabControl1.MainPage = "Avatar";
            //设置初始页面索引（关联页面，唯一不重复即可）
            int pageIndex = 1000;

            //uiNavBar1设置节点，也可以在Nodes属性里配置
            uiNavBar1.Nodes.Add("控件");
            uiNavBar1.Nodes.Add("窗体");
            uiNavBar1.Nodes.Add("图表");
            uiNavBar1.Nodes.Add("工控");
            uiNavBar1.Nodes.Add("主题");
            uiNavBar1.SetNodePageIndex(uiNavBar1.Nodes[0], pageIndex);
            uiNavBar1.SetNodeSymbol(uiNavBar1.Nodes[0], 61451);
            TreeNode parent = uiNavMenu1.CreateNode("控件", 61451, 24, pageIndex);
            

            //通过设置PageIndex关联，节点文字、图标由相应的Page的Text、Symbol提供
            uiNavMenu1.CreateChildNode(parent, AddPage(new FAvatar(), ++pageIndex));
            pageSet[pageIndex] = "Sunny.UI.Demo.FAvatar";
            uiNavMenu1.CreateChildNode(parent, AddPage(new FButton(), ++pageIndex));
            pageSet[pageIndex] = "Sunny.UI.Demo.FButton";



            uiNavBar1.SetNodeSymbol(uiNavBar1.Nodes[4], 61502);
            System.Collections.Generic.List<UIStyle> styles = UIStyles.PopularStyles();
            foreach (UIStyle style in styles)
            {
                uiNavBar1.CreateChildNode(uiNavBar1.Nodes[4], style.Description(), style.Value());
            }

            var node = uiNavBar1.CreateChildNode(uiNavBar1.Nodes[4], "字体图标", 99999);
            uiNavBar1.SetNodeSymbol(node, 558426);
            node = uiNavBar1.CreateChildNode(uiNavBar1.Nodes[4], "多彩主题", UIStyle.Colorful.Value());
            uiNavBar1.SetNodeSymbol(node, 558295);

            //左侧导航主节点关联页面
            uiNavMenu1.CreateNode(AddPage(new FSymbols(), 99999));
            pageSet[99999] = "Sunny.UI.Demo.FSymbols";
            uiNavMenu1.CreateNode(AddPage(new FColorful(), UIStyle.Colorful.Value()));
            pageSet[UIStyle.Colorful.Value()] = "Sunny.UI.Demo.FColorful";

            //根据页面索引获取页面
            UIPage page1 = GetPage(1002);
            if (page1 != null)
                page1.Text.WriteConsole();

        }

        private void uiNavMenu1_MenuItemClick(TreeNode node, NavMenuItem item, int pageIndex)
        {
            if (!ExistPage(pageIndex))
            {
                UIPage page;
                if (pageSet[pageIndex].Contains("FSymbols"))
                {
                    page = new FSymbols();
                    
                }
                else 
                {
                     page = CreateInstance<UIPage>(pageSet[pageIndex]);
                    
                }
                page.Render();
                AddPage(page, pageIndex);//反射创建对象
            }
            else
            {
                MainTabControl.GetPage(pageIndex).Render();
            }
            
            SelectPage(pageIndex);
            
            DateTime now = DateTime.Now;



            Console.WriteLine(now.ToString());
        }

        /// <summary>
        /// 创建对象实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fullName">命名空间.类型名</param>
        /// <returns></returns>
        public static T CreateInstance<T>(string fullName)
        {
            Type o = Type.GetType(fullName);
            dynamic obj = Activator.CreateInstance(o, true);
            return (T)obj;//类型转换并返回
        }

        private bool uiTabControl1_BeforeRemoveTabPage(object sender, int index)
        {
            return this.ShowAskDialog("你确定要关闭 " + uiTabControl1.TabPages[index].Text + "选项卡吗?");
        }

        private void 天空蓝ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StyleManager.Style = (UIStyle)1;
        }

        private void 清新绿ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StyleManager.Style = (UIStyle)2;
        }

        private void 科技蓝ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StyleManager.Style = (UIStyle)101;
        }

        private void uiNavBar1_MenuItemClick(string itemText, int menuIndex, int pageIndex)
        {
            switch (menuIndex)
            {
                case 4:
                    UIStyle style = (UIStyle)pageIndex;
                    if (pageIndex < UIStyle.Colorful.Value())
                        StyleManager.Style = style;
                    else
                        uiNavMenu1.SelectPage(pageIndex);
                    break;
                default:
                    uiNavMenu1.SelectPage(pageIndex);
                    break;
            }
            this.Render();
        }       
    }
}
